// MainFrm.h : interface of the CMainFrame class
//
/////////////////////////////////////////////////////////////////////////////

#pragma once
#include <atlcrack.h>
#include "ChildViewAnimate.h"
#include "SlideFormView.h"
#include "SlideTreeView.h"
#include "SlideListView.h"
#include "SelectionBar.h"


class CMainFrame :	public CFrameWindowImpl<CMainFrame>, 
					public CUpdateUI<CMainFrame>,
					public CAppWindow<CMainFrame>,
					public CChildViewAnimate<CMainFrame>,
					public CMessageFilter, 
					public CIdleHandler
{
private:
	CSlideFormView	m_viewForm;
	CSlideTreeView	m_viewTree;
	CSlideListView	m_viewList;
#ifdef WIN32_PLATFORM_PSPC
	CSelectionBar	m_selBar;
#endif
	ViewAnimation	m_nAnimate;

	void UpdateAnimateMenu(int nID)
	{
		UISetCheck(ID_ANIMATE_LEFT,		nID == ID_ANIMATE_LEFT	);
		UISetCheck(ID_ANIMATE_RIGHT,	nID == ID_ANIMATE_RIGHT	);
		UISetCheck(ID_ANIMATE_UP,		nID == ID_ANIMATE_UP	);
		UISetCheck(ID_ANIMATE_DOWN,		nID == ID_ANIMATE_DOWN	);
		UISetCheck(ID_ANIMATE_NONE,		nID == ID_ANIMATE_NONE	);
	}

public:

	DECLARE_FRAME_WND_CLASS(NULL, IDR_MAINFRAME)

	CMainFrame()
	:	m_nAnimate	(None)
	{
	}

	virtual BOOL PreTranslateMessage(MSG* pMsg)
	{
		if(CFrameWindowImpl<CMainFrame>::PreTranslateMessage(pMsg))
			return TRUE;

		return ChildPreTranslateMessage(pMsg);
	}

	virtual BOOL OnIdle()
	{
		return FALSE;
	}

	BEGIN_UPDATE_UI_MAP(CMainFrame)
		UPDATE_ELEMENT(ID_ANIMATE_LEFT,		UPDUI_MENUPOPUP)
		UPDATE_ELEMENT(ID_ANIMATE_RIGHT,	UPDUI_MENUPOPUP)
		UPDATE_ELEMENT(ID_ANIMATE_UP,		UPDUI_MENUPOPUP)
		UPDATE_ELEMENT(ID_ANIMATE_DOWN,		UPDUI_MENUPOPUP)
		UPDATE_ELEMENT(ID_ANIMATE_NONE,		UPDUI_MENUPOPUP)
	END_UPDATE_UI_MAP()

	BEGIN_MSG_MAP(CMainFrame)
		MESSAGE_HANDLER(WM_CREATE, OnCreate)
		COMMAND_ID_HANDLER(ID_ACTION, OnAction)
		COMMAND_ID_HANDLER(ID_APP_EXIT, OnFileExit)
		COMMAND_ID_HANDLER(ID_FILE_NEW, OnFileNew)
		COMMAND_ID_HANDLER(ID_APP_ABOUT, OnAppAbout)
		COMMAND_ID_HANDLER_EX(ID_VIEW_LIST, OnViewList)
		COMMAND_ID_HANDLER_EX(ID_VIEW_TREE, OnViewTree)
		COMMAND_ID_HANDLER_EX(ID_VIEW_FORM, OnViewForm)
		COMMAND_ID_HANDLER_EX(ID_ANIMATE_DOWN, OnAnimateDown)
		COMMAND_ID_HANDLER_EX(ID_ANIMATE_LEFT, OnAnimateLeft)
		COMMAND_ID_HANDLER_EX(ID_ANIMATE_NONE, OnAnimateNone)
		COMMAND_ID_HANDLER_EX(ID_ANIMATE_RIGHT, OnAnimateRight)
		COMMAND_ID_HANDLER_EX(ID_ANIMATE_UP, OnAnimateUp)
		CHAIN_MSG_MAP(CUpdateUI<CMainFrame>)
		CHAIN_MSG_MAP(CFrameWindowImpl<CMainFrame>)

		REFLECT_NOTIFICATIONS()
	END_MSG_MAP()

// Handler prototypes (uncomment arguments if needed):
//	LRESULT MessageHandler(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/)
//	LRESULT CommandHandler(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
//	LRESULT NotifyHandler(int /*idCtrl*/, LPNMHDR /*pnmh*/, BOOL& /*bHandled*/)

	LRESULT OnCreate(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/)
	{
		CreateSimpleCEMenuBar(IDR_MAINFRAME, SHCMBF_HMENU);

		m_hWndClient	= m_viewForm.Create(m_hWnd);

#ifdef WIN32_PLATFORM_PSPC
		m_hWndToolBar	= m_selBar.Create(m_hWnd);
		if(m_hWndToolBar)
		{
			m_selBar.SetTitle	(SBB_LEFT,  _T("Animate"));
			m_selBar.SetOption	(SBB_LEFT,	_T("None"));
			m_selBar.SetMenu	(SBB_LEFT,	MAKEINTRESOURCE(IDR_ANIMATE));

			m_selBar.SetTitle	(SBB_RIGHT, _T("Views"));
			m_selBar.SetOption	(SBB_RIGHT, _T("Form"));
			m_selBar.SetMenu	(SBB_RIGHT, MAKEINTRESOURCE(IDR_VIEWS));
		}
#endif

		m_pView = &m_viewForm;

		// register object for message filtering and idle updates
		CMessageLoop* pLoop = _Module.GetMessageLoop();
		ATLASSERT(pLoop != NULL);
		pLoop->AddMessageFilter(this);
		pLoop->AddIdleHandler(this);

		UpdateAnimateMenu(ID_ANIMATE_NONE);

		return 0;
	}

	LRESULT OnAction(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
	{
		// TODO: add code

		return 0;
	}

	LRESULT OnFileExit(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
	{
		PostMessage(WM_CLOSE);
		return 0;
	}

	LRESULT OnFileNew(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
	{
		// TODO: add code to initialize document

		return 0;
	}

	LRESULT OnAppAbout(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
	{
		CAboutDlg dlg;
		dlg.DoModal();
		return 0;
	}

	LRESULT OnViewList(WORD wNotifyCode, WORD wID, HWND hWndCtl)
	{
		HWND	hWnd	= m_viewList.m_hWnd;

		if(hWnd == NULL)
		{
			int		nWidth;
			DWORD	dwStyle	= WS_CHILD | WS_VISIBLE | WS_CLIPSIBLINGS | WS_CLIPCHILDREN | LVS_REPORT | LVS_SHOWSELALWAYS;
			CRect	rc;
			CWindow	wndView(*m_pView);

			wndView.GetClientRect(&rc);

			nWidth		= rc.Width();
			rc.left		+= nWidth;
			rc.right	+= nWidth;

			hWnd = m_viewList.Create(m_hWnd, &rc, NULL, dwStyle);
			if(hWnd)
			{
				m_viewList.InsertColumn(0, _T("View"), LVCFMT_LEFT, 80, -1);

				m_viewList.InsertItem(0, _T("Form"));
				m_viewList.InsertItem(1, _T("List"));
				m_viewList.InsertItem(2, _T("Tree"));
			}
		}

		if(hWnd != NULL)
		{
			SwitchToView(&m_viewList, m_nAnimate);
#ifdef WIN32_PLATFORM_PSPC
			m_selBar.SetOption(SBB_RIGHT, _T("List"));
#endif
		}

		return 0;
	}

	LRESULT OnViewTree(WORD wNotifyCode, WORD wID, HWND hWndCtl)
	{
		HWND hWnd = m_viewTree.m_hWnd;

		if(hWnd == NULL)
		{
			int		nWidth;
			DWORD	dwStyle	= WS_CHILD | WS_VISIBLE | WS_CLIPSIBLINGS | WS_CLIPCHILDREN;// | TVS_HASBUTTONS | TVS_HASLINES | TVS_LINESATROOT | TVS_SHOWSELALWAYS;
			CRect	rc;
			CWindow	wndView(*m_pView);

			wndView.GetClientRect(&rc);

			nWidth		= rc.Width();
			rc.left		+= nWidth;
			rc.right	+= nWidth;

			hWnd = m_viewTree.Create(m_hWnd, &rc, NULL, dwStyle);
			if(hWnd)
			{
			}
		}

		if(hWnd != NULL)
		{
			SwitchToView(&m_viewTree, m_nAnimate);
#ifdef WIN32_PLATFORM_PSPC
			m_selBar.SetOption(SBB_RIGHT, _T("Tree"));
#endif
		}

		return 0;
	}

	LRESULT OnViewForm(WORD wNotifyCode, WORD wID, HWND hWndCtl)
	{
		SwitchToView(&m_viewForm, m_nAnimate);
#ifdef WIN32_PLATFORM_PSPC
		m_selBar.SetOption(SBB_RIGHT, _T("Form"));
#endif
		return 0;
	}

	LRESULT OnAnimateDown(WORD wNotifyCode, WORD wID, HWND hWndCtl)
	{
		m_nAnimate = ScrollDown;
#ifdef WIN32_PLATFORM_PSPC
		m_selBar.SetOption(SBB_LEFT, _T("Down"));
#endif
		UpdateAnimateMenu(ID_ANIMATE_DOWN);
		return 0;
	}

	LRESULT OnAnimateLeft(WORD wNotifyCode, WORD wID, HWND hWndCtl)
	{
		m_nAnimate = ScrollLeft;
#ifdef WIN32_PLATFORM_PSPC
		m_selBar.SetOption(SBB_LEFT, _T("Left"));
#endif
		UpdateAnimateMenu(ID_ANIMATE_LEFT);
		return 0;
	}

	LRESULT OnAnimateNone(WORD wNotifyCode, WORD wID, HWND hWndCtl)
	{
		m_nAnimate = None;
#ifdef WIN32_PLATFORM_PSPC
		m_selBar.SetOption(SBB_LEFT, _T("None"));
#endif
		UpdateAnimateMenu(ID_ANIMATE_NONE);
		return 0;
	}

	LRESULT OnAnimateRight(WORD wNotifyCode, WORD wID, HWND hWndCtl)
	{
		m_nAnimate = ScrollRight;
#ifdef WIN32_PLATFORM_PSPC
		m_selBar.SetOption(SBB_LEFT, _T("Right"));
#endif
		UpdateAnimateMenu(ID_ANIMATE_RIGHT);
		return 0;
	}

	LRESULT OnAnimateUp(WORD wNotifyCode, WORD wID, HWND hWndCtl)
	{
		m_nAnimate = ScrollUp;
#ifdef WIN32_PLATFORM_PSPC
		m_selBar.SetOption(SBB_LEFT, _T("Up"));
#endif
		UpdateAnimateMenu(ID_ANIMATE_UP);
		return 0;
	}
};
